package lanou3g.myfirstapp36kr.fragment;

import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore.Images.Media;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;
import lanou3g.myfirstapp36kr.MyUser;
import lanou3g.myfirstapp36kr.activity.AccountMessageActivity;
import lanou3g.myfirstapp36kr.activity.ColectionActivity;
import lanou3g.myfirstapp36kr.activity.LoginActivity;
import lanou3g.myfirstapp36kr.activity.MoneyQuickActivity;
import lanou3g.myfirstapp36kr.activity.MyMoneyActivity;
import lanou3g.myfirstapp36kr.activity.MyMoneyCompanyActivity;
import lanou3g.myfirstapp36kr.activity.MyOrderActivity;
import lanou3g.myfirstapp36kr.R;
import lanou3g.myfirstapp36kr.activity.SettingActivity;
import lanou3g.myfirstapp36kr.base.BaseFragment;
import lanou3g.myfirstapp36kr.srcollView.CircleDrawable;

/**
 * Created by dllo on 16/8/2.
 */
public class MyFragment extends BaseFragment {
    private ImageView imageViewSetting;
    private FrameLayout frameLayout;
    private ImageView imageViewqq;
    private PicChangedReceiver picChangedReceiver;
    private Bitmap bitmap;
    private MyUser myUser;

    @Override
    protected int getLayout() {
        return R.layout.fragment_my;
    }


    @Override
    protected void initView(View view) {
        picChangedReceiver = new PicChangedReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(AccountMessageActivity.ACTION_CHANGE);
        intentFilter.addAction(SettingActivity.GO_OUT);
        this.getActivity().registerReceiver(picChangedReceiver,intentFilter);
        myUser = BmobUser.getCurrentUser(MyUser.class);



        imageViewqq = (ImageView) view.findViewById(R.id.iv_qq);
        frameLayout = (FrameLayout) view.findViewById(R.id.frame_layout_tell);

        imageViewSetting = (ImageView) view.findViewById(R.id.iv_fragmentmy_setting);
        imageViewSetting.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MyFragment.this.getActivity(), SettingActivity.class);
                startActivity(intent);
            }
        });
        view.findViewById(R.id.know_money).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                BmobUser bmobUser = BmobUser.getCurrentUser();
                if (bmobUser != null){
                    Intent intent = new Intent(MyFragment.this.getActivity(),MoneyQuickActivity.class);
                    startActivity(intent);

                }else {
                    Intent intent = new Intent(MyFragment.this.getActivity(),LoginActivity.class);
                    startActivity(intent);

                }

            }
        });
        view.findViewById(R.id.my_colection).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                BmobUser bmobUser = BmobUser.getCurrentUser();
                if (bmobUser != null){
                    Intent intent = new Intent(MyFragment.this.getActivity(), ColectionActivity.class);
                    startActivity(intent);

                }else {
                    Intent intent = new Intent(MyFragment.this.getActivity(),LoginActivity.class);
                    startActivity(intent);

                }


            }
        });
        view.findViewById(R.id.my_order).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {

                BmobUser bmobUser = BmobUser.getCurrentUser();
                if (bmobUser != null){
                    Intent intent = new Intent(MyFragment.this.getActivity(), MyOrderActivity.class);
                    startActivity(intent);

                }else {
                    Intent intent = new Intent(MyFragment.this.getActivity(),LoginActivity.class);
                    startActivity(intent);

                }

            }
        });
        view.findViewById(R.id.account_message).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {

                BmobUser bmobUser = BmobUser.getCurrentUser();
                if (bmobUser != null){
                    Intent intent = new Intent(MyFragment.this.getActivity(), AccountMessageActivity.class);
                    startActivity(intent);

                }else {
                    Intent intent = new Intent(MyFragment.this.getActivity(),LoginActivity.class);
                    startActivity(intent);

                }

            }
        });
        view.findViewById(R.id.money_sure).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                BmobUser bmobUser = BmobUser.getCurrentUser();
                if (bmobUser != null){
                    Intent intent = new Intent(MyFragment.this.getActivity(),MoneyQuickActivity.class);
                    startActivity(intent);

                }else {
                    Intent intent = new Intent(MyFragment.this.getActivity(),LoginActivity.class);
                    startActivity(intent);

                }


            }
        });
        view.findViewById(R.id.my_money_company).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                BmobUser bmobUser = BmobUser.getCurrentUser();
                if (bmobUser != null){
                    Intent intent = new Intent(MyFragment.this.getActivity(),MyMoneyCompanyActivity.class);
                    startActivity(intent);

                }else {
                    Intent intent = new Intent(MyFragment.this.getActivity(),LoginActivity.class);
                    startActivity(intent);

                }

            }
        });
        view.findViewById(R.id.my_money).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                BmobUser bmobUser = BmobUser.getCurrentUser();
                if (bmobUser != null){
                    Intent intent = new Intent(MyFragment.this.getActivity(), MyMoneyActivity.class);
                    startActivity(intent);

                }else {
                    Intent intent = new Intent(MyFragment.this.getActivity(),LoginActivity.class);
                    startActivity(intent);

                }

            }
        });
        view.findViewById(R.id.tell).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                frameLayout.setVisibility(View.VISIBLE);

            }
        });
        view.findViewById(R.id.tv_cancel).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                frameLayout.setVisibility(View.GONE);
            }
        });

        //拨打电话
        view.findViewById(R.id.tv_tell_custom).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_CALL);
                Uri data = Uri.parse("tel:" + "4009953636");
                intent.setData(data);
                startActivity(intent);

            }
        });

    }

    @Override
    protected void initData() {
        SharedPreferences sharedPreferences = this.getActivity().getPreferences(Context.MODE_PRIVATE);
        String path = sharedPreferences.getString("path","");
        if(path == ""){
            imageViewqq.setImageResource(R.mipmap.ic_launcher);
        }else {
            bitmap = BitmapFactory.decodeFile(path);
            imageViewqq.setImageBitmap(bitmap);

        }

        if (myUser != null){
            Bitmap bitmapNet = myUser.getIcon();
            if (bitmapNet != null){
                imageViewqq.setImageBitmap(bitmapNet);
            }
        }else {
            imageViewqq.setImageResource(R.mipmap.ic_launcher);
        }




    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.d("MyFragment", "resultCode:" + resultCode);
        if (requestCode == 101 && resultCode == -1);
        //重新获取的URI指向单独选择的那张图片
        Uri uri = data.getData();
        //通过ContentResolver进行查询
        ContentResolver cr = this.getActivity().getContentResolver();
        Cursor cursor = cr.query(uri,null,null,null,null);
        Log.d("MyFragment", "aaa");
        //查询之后Curson对象中只有一条数据
        if (cursor != null && cursor.moveToFirst()){
            //获取到该图片存贮的路径
            String path = cursor.getString(cursor.getColumnIndex(
                    Media.DATA
            ));
            bitmap = BitmapFactory.decodeFile(path);
            Log.d("MyFragment", "ssss");
            imageViewqq.setImageBitmap(bitmap);
            SharedPreferences sharedPreferences = this.getActivity().getPreferences(Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString("path",path);
            editor.apply();

            if (myUser != null){
                Log.d("MyFragment", "myUser != null");
                myUser.setIcon(bitmap);
                myUser.update(new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        if(e == null){
                            Log.d("MyFragment", "上传成功");
                        }else {
                            Log.d("MyFragment", "上传失败:" + e.getMessage());
                        }
                    }
                });

            }else {
                Log.d("MyFragment", "myUser == null");
            }


        }
    }

    public class PicChangedReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action){
                case AccountMessageActivity.ACTION_CHANGE:


                        Intent intentPic = new Intent();
                        //动作  选择
                        intentPic.setAction(intent.ACTION_PICK);
                        //通过URI 指定跳转到系统图库
                        intentPic.setData(Media.EXTERNAL_CONTENT_URI);
                        startActivityForResult(intentPic,101);

                    break;
                case SettingActivity.GO_OUT:
                    imageViewqq.setImageResource(R.mipmap.ic_launcher);
            }

        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.getActivity().unregisterReceiver(picChangedReceiver);
    }
}
